//
//  HeroController.m
//  MyDota
//
//  Created by 濮亚军 on 15/12/2.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "HeroController.h"

@interface HeroController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//设置collectionView
@property(nonatomic, strong)UICollectionView *heroCollection;

//model数组
@property(nonatomic, strong)NSArray *heroModelArray;

//详情视图
@property(nonatomic, strong)HeroDetailView *heroDetailView;

@end
static NSString *cellIdentifier = @"cellIdentifier";
@implementation HeroController
{
    CGFloat width;
    CGFloat height;
    CGFloat x;
}
#pragma mark --------获取model数组--------
-(void)getModelArray
{
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Hero"];
    //查找Hero表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        self.heroModelArray = array;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.heroCollection reloadData];
        });
    }];
}
#pragma mark --------设置collectionView--------
-(void)setCollection
{
    //设置collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.heroCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion) collectionViewLayout:layout];
    self.heroCollection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_heroCollection];
    //设置代理
    self.heroCollection.dataSource = self;
    self.heroCollection.delegate = self;
    
    [self.heroCollection registerClass:[HeroCollectionCell class] forCellWithReuseIdentifier:cellIdentifier];
}
#pragma mark --------添加详情子视图--------
-(void)setHeroDetailView
{
    self.heroDetailView = [[HeroDetailView alloc]initWithFrame:CGRectMake(0, -(self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion), kScreenWidth, self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion)];
    //self.heroDetailView.backgroundColor = [UIColor redColor];
    self.heroDetailView.hidden = YES;
    [self.view addSubview:_heroDetailView];
    
    UITapGestureRecognizer *heroDetailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(heroDetailTapAction:)];
    [self.heroDetailView addGestureRecognizer:heroDetailTap];
}
#pragma mark --------详情子视图返回手势--------
-(void)heroDetailTapAction:(UITapGestureRecognizer *)heroDetailTap
{
    
    [UIView animateWithDuration:1 animations:^{
        self.heroDetailView.frame = CGRectMake(0, self.view.frame.size.height, kScreenWidth, self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion);
    } completion:^(BOOL finished) {
        self.heroDetailView.frame = CGRectMake(0, -(self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion), kScreenWidth, self.view.frame.size.height - 64 - 64 * kHProportion - 35 * kHProportion);
        self.heroDetailView.hidden = YES;
        [self.heroDetailView hidden];
    }];
}
#pragma mark --------viewDidLoad--------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取数组
    [self getModelArray];
    
    width = (kScreenWidth - 60 * kWProportion) / 4;
    height = width + 30 * kHProportion;
    NSLog(@"width === %f  height ==== %f",width,height);
    x = 10 * kWProportion;
    //加载UICollectionView
    [self setCollection];
    
    //英雄详情介绍
    [self setHeroDetailView];
}

#pragma mark --------collectionView代理方法--------
//分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//分区下的cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.heroModelArray.count;
}
//返回cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeroCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.heroModel = [[HeroModel alloc]initWitnBmobObject:self.heroModelArray[indexPath.row]];
    return cell;
}
//点击cell方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"详细介绍");
    self.heroDetailView.heroModel = [[HeroModel alloc]initWitnBmobObject:self.heroModelArray[indexPath.row]];
    [UIView animateWithDuration:1 animations:^{
        self.heroDetailView.hidden = NO;
        self.heroDetailView.frame = CGRectMake(0, 0, kScreenWidth, self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [self.heroDetailView shows];
    }];
}
//返回cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(width, height);
}
//返回行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return x;
}
//返回列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return x;
}
//返回距离边的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(x, x, x, x);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
